package d20chat;


import java.util.regex.*;


public class MessageInfo
{
    /**
     * the message to gather info about
     */
    public String message = "";
    /**
     * the name of the person who sent the message.
     */
    public String originName = "";
    /**
     * chat, command, or request.
     * chat is plain messages between people
     * commands come from the gm
     * requests effect what the program does and can come from anyone
     */
    public String type = "";
    /**
     * identifier for the type of message.
     */
    public String subType = "";
    /**
     * who the message is about.
     */
    public String targetName = "";
    /**
     * used for on off states for charactersheet and silence.
     */
    public String state = "";
    /**
     * for the text of messages, only part of message more than one word long.
     */
    public String text = "";
    public int endOfWord1;
    public int endOfWord2;
    public int endOfWord3;
    public int endOfWord4;
    public int endOfWord5;
    
    public String toString()
    {
        return originName + " " + type + " " + subType + " " + targetName + " " + text + " ";
    }
    
    public void seperate(String aMessage)
    {    
        //store the whole message
        message = aMessage;
        
        //stores a compiled regular expression that pulls out words
        Pattern wordPattern = Pattern.compile( ".*?\\s" );
        //object to connect our word expression to our message
        Matcher matcher = wordPattern.matcher( message );
        
        //find the first word in the message
        if( matcher.find() )
        {
            //this word will be the name of the person with whom the message originated
            originName = message.substring(matcher.start(), matcher.end()-1);
            endOfWord1 = matcher.end();
        }
        
        //find the next word in the message
        if( matcher.find() )
        {
            //this word will be either chat or command
            type = message.substring(matcher.start(), matcher.end()-1);
            endOfWord2 = matcher.end();
        }
        
        //find the next word
        if( matcher.find() )
        {
            //this word will be the subtype such as tell or say for chat or silence for command
            subType = message.substring(matcher.start(), matcher.end()-1);
            endOfWord3 = matcher.end();
        }
        
        //find the next word
        if( matcher.find() )
        {
            //this word will be the name of the person the message is about
            targetName = message.substring(matcher.start(), matcher.end()-1);
            endOfWord4 = matcher.end();
        }
        
        //find the next word
        if( matcher.find() )
        {
            //this word will be a state message for some commands such as on or off for silence
            state = message.substring(matcher.start(), matcher.end()-1);
            endOfWord5 = matcher.end();
        }
    }
    
    
    public MessageInfo()
    {
    }
    
    
    public MessageInfo(String aMessage)
    {
        seperate(aMessage);
    }
    
}


